﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;

namespace WMSSoft.WinKStart
{
    /// <summary>
    /// Interaktionslogik für Settings_ViewDataSourceData.xaml
    /// </summary>
    public partial class Settings_ViewDataSourceData : Window
    {

        private DataSource _DataSourceToView = null;        

        public DataSource DataSourceToView
        {
            set { _DataSourceToView = value; }
        }



        public Settings_ViewDataSourceData()
        {
            InitializeComponent();
        }

        public Settings_ViewDataSourceData(DataSource DataSourceToView)
        {
            _DataSourceToView = DataSourceToView;
      
            InitializeComponent();
        }

        private void btn_CloseDlg_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Load viewed Items from the DataSource. The Folder where written to the TreeView on left dialog side,
        /// the other Items to the ListView in right Dialog side
        /// </summary>
        private void LoadDataSourceData()
        {
            try
            {
                lbl_DataSourceName.Content = "View DataSource '" + _DataSourceToView.ShortName + "'";
                cStartmenuItem oDataSourceItem = cMenuConfigReader.DataSources.GetData(_DataSourceToView.Name, "");

                TreeViewItem oRootItem = new TreeViewItem();
                oRootItem.Header = "DataSource '" + _DataSourceToView.ShortName + "'";
                oRootItem.Name = "DATASOURCEVIEWER_ROOTITEM";
                trv_DataSourceFolders.Items.Add(oRootItem);

                Dictionary<string, Object>.KeyCollection oKeyItems = oDataSourceItem.ChildItems.Keys;
                //Iterate each item
                foreach (string KeyItem in oKeyItems)
                {
                    if (((cStartmenuItem)oDataSourceItem.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Folder ||
                        ((cStartmenuItem)oDataSourceItem.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Root ||
                        ((cStartmenuItem)oDataSourceItem.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Filter)
                    {
                        TreeViewItem oFolderItem = new TreeViewItem();
                        oFolderItem.Name = ((cStartmenuItem)oDataSourceItem.ChildItems[KeyItem]).ItemName;
                        //oFolderItem.Header = ((cStartmenuItem)oDataSourceItem.ChildItems[KeyItem]).ViewName;
                        oFolderItem.Tag = ((cStartmenuItem)oDataSourceItem.ChildItems[KeyItem]).FolderMenuPath;

                        BitmapImage myBImg = basic_GraphicHelpers.WPFBitmapFromDrawingImage(Properties.Resources.Folder);
                        StackPanel myStackpanel = new StackPanel();
                        TextBlock myTextblock = new TextBlock();
                        Image myImage = new Image();
                        myImage.Height = 16;
                        myImage.Width = 16;
                        myImage.Stretch = Stretch.Uniform;

                        myImage.Source = myBImg;
                        myImage.Margin = new Thickness(0.0, 0.0, 0.0, 0.0);
                        myStackpanel.Children.Add(myImage);

                        myTextblock.Text = ((cStartmenuItem)oDataSourceItem.ChildItems[KeyItem]).ViewName;

                        myStackpanel.Children.Add(myTextblock);
                        myStackpanel.Orientation = Orientation.Horizontal;

                        oFolderItem.Header = myStackpanel;

                        oRootItem.Items.Add(oFolderItem);
                        //Read all subfolders
                        if (((cStartmenuItem)oDataSourceItem.ChildItems[KeyItem]).ChildItems.Count > 0)
                        {
                            ReadSubFolders((cStartmenuItem)oDataSourceItem.ChildItems[KeyItem], oFolderItem);
                        }
                    }
                }
                //Expand Root Node
                oRootItem.ExpandSubtree();
            }
            catch (Exception Ex)
            {
                string sNameDataSource = "";
                if (_DataSourceToView != null)
                    sNameDataSource = _DataSourceToView.ShortName;

                CMessagelogging.AppendMessage("Loading Data for DataSource '" + sNameDataSource + "' failed!", Ex, true, EMessageTypes.Error);
            }
        }

        private void ReadSubFolders(cStartmenuItem Parent, TreeViewItem NodeParent)
        {
            try
            {
                if (Parent == null || NodeParent == null) return;

                Dictionary<string, Object>.KeyCollection oKeyItems = Parent.ChildItems.Keys;
                foreach (string KeyItem in oKeyItems)
                {
                    if (((cStartmenuItem)Parent.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Folder ||
                        ((cStartmenuItem)Parent.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Root ||
                        ((cStartmenuItem)Parent.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Filter)
                    {
                        TreeViewItem oFolderItem = new TreeViewItem();
                        oFolderItem.Name = ((cStartmenuItem)Parent.ChildItems[KeyItem]).ItemName;
                        //oFolderItem.Header = ((cStartmenuItem)Parent.ChildItems[KeyItem]).ViewName;
                        oFolderItem.Tag = ((cStartmenuItem)Parent.ChildItems[KeyItem]).FolderMenuPath;

                        BitmapImage myBImg = basic_GraphicHelpers.WPFBitmapFromDrawingImage(Properties.Resources.Folder);
                        StackPanel myStackpanel = new StackPanel();
                        TextBlock myTextblock = new TextBlock();
                        Image myImage = new Image();
                        myImage.Height = 16;
                        myImage.Width = 16;
                        myImage.Stretch = Stretch.Uniform;

                        myImage.Source = myBImg;
                        myImage.Margin = new Thickness(0.0, 0.0, 0.0, 0.0);
                        myStackpanel.Children.Add(myImage);

                        myTextblock.Text = ((cStartmenuItem)Parent.ChildItems[KeyItem]).ViewName;

                        myStackpanel.Children.Add(myTextblock);
                        myStackpanel.Orientation = Orientation.Horizontal;

                        oFolderItem.Header = myStackpanel;

                        NodeParent.Items.Add(oFolderItem);
                        //Read all subfolders
                        if (((cStartmenuItem)Parent.ChildItems[KeyItem]).ChildItems.Count > 0)
                        {
                            ReadSubFolders((cStartmenuItem)Parent.ChildItems[KeyItem], oFolderItem);
                        }
                    }
                }

            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error while processing DadaSource-Data - Folder!", Ex, true, EMessageTypes.Error);
            }
        }

        /// <summary>
        /// Show Link Items for the Selected DataSource Folder in ListView
        /// </summary>
        /// <param name="Path"></param>
        private void SelectFolder(string Path)
        {
            try
            {
                string sTmpFolderPath = "";
                if (Path != null) sTmpFolderPath = Path;

                lv_DataSourceFolderItems.Items.Clear();

                cStartmenuItem oDataSourceSelectedFolderItem = cMenuConfigReader.DataSources.GetData(_DataSourceToView.Name, sTmpFolderPath);
                if (oDataSourceSelectedFolderItem != null && oDataSourceSelectedFolderItem.ChildItems.Count > 0)
                {
                    lbl_SelectedFolderPath.Content = Path;

                    Dictionary<string, Object>.KeyCollection oKeyItems = oDataSourceSelectedFolderItem.ChildItems.Keys;

                    foreach (string KeyItem in oKeyItems)
                    {
                        if (((cStartmenuItem)oDataSourceSelectedFolderItem.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Link)
                        {
                            lv_DataSourceFolderItems.Items.Add((cStartmenuItem)oDataSourceSelectedFolderItem.ChildItems[KeyItem]);
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error while selecting Folder to view Items!", Ex, true, EMessageTypes.Error);
            }
        }

        void Localise()
        {
            try
            {
                ((GridView)lv_DataSourceFolderItems.View).Columns[0].Header = LanguageStrings.GetText("View-name");
                ((GridView)lv_DataSourceFolderItems.View).Columns[1].Header = LanguageStrings.GetText("Command");
                ((GridView)lv_DataSourceFolderItems.View).Columns[2].Header = LanguageStrings.GetText("Arguments");
                ((GridView)lv_DataSourceFolderItems.View).Columns[3].Header = LanguageStrings.GetText("Working Directory");
                btn_CloseDlg.Content = LanguageStrings.GetText("Close");
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Can't set localized strings!", Ex, true, EMessageTypes.Error);
            }
        }

        private void SettingsDlg_ViewDataSourceData_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDataSourceData();
            Localise();
        }

        private void trv_DataSourceFolders_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                if (trv_DataSourceFolders.SelectedItem != null)
                {
                    SelectFolder((String)((TreeViewItem)trv_DataSourceFolders.SelectedItem).Tag);
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error while selecting folder!", Ex, true, EMessageTypes.Error);
            }
        }

        private void SettingsDlg_ViewDataSourceData_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }
    }
}
